<?php
App::uses('AppModel', 'Model');
App::uses('AuthComponent', 'Controller/Component');

class Post extends AppModel {
	/*
	public $hasMany = array(
        'Image' => array(
            'className' => 'Image',
            'foreignKey' => 'post_id',
        	'fields' =>array('Image.url'),
            'conditions' => array('Image.deleted' => '0'
        )
    ));
    */

	/**
	* Get posts of id
	* @author: ThanhTran
	* @since 2014-11-08
	* @return 1 post
	*/
	public function findByShortUrl($short_url = null){
		$sql = array(
                        'conditions' => array(
                        						'short_url' => $short_url,
                        						'check_status' =>1
                        					)
					);
		return $this->find('first', $sql);
	}
	/**
	* Get posts by user_id
	* @author: ThanhTran
	* @since 2014-12-016
	* @return 1 post
	*/
	public function findByUserId($userId = null){
		$sql = array(
                        'conditions' => array(
                        						'user_id' => $userId
                        					)
					);
		return $this->find('all', $sql);
	}

	/**
	* Get posts by category_id
	* @author: ThanhTran
	* @since 2014-12-016
	* @return 1 post
	*/
	public function findByCategory($categoryId = null){
		$sql = array(
                        'conditions' => array(
                        						'category_ids' => $categoryId,
                        						'check_status' =>1
                        					)
					);
		return $this->find('all', $sql);
	}


	/**
	* Get posts by post ID
	* @author: QuocTran
	* @since 2014-11-08
	* @return post
	*/
	public function findById($id){
		$sql = array('conditions' => array('Post.id' => $id));
		return $this->find('first', $sql);
	}

	// public function beforeSave($userId = 1, $options = array()) {
		// if(empty($this->data['Post']['id'])){
			// $this->data['Post']['created_by'] = 1;
			// $this->data['Post']['updated_by'] = 1;
			// $this->data['Post']['created_at'] = date("Y-m-d H:i:s");
		// }else{
			// $this->data['Post']['updated_by'] = $userId;
		// }
		// $this->data['Post']['updated_at'] = date("Y-m-d H:i:s");
	// }
		/**
	* Get posts by post ID
	* @author: QuocTran
	* @since 2014-11-08
	* @return post
	*/
	public function findByIdAndCheckStatus($id, $checkStatus){
		$sql = array('conditions' => array('Post.id' => $id, "Post.check_status"=>$checkStatus));
		return $this->find('first', $sql);
	}

}

